package com.project.base.baseproject.Activity;
import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.SystemClock;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.jaeger.library.StatusBarUtil;
import com.project.base.baseproject.Activity.LoginActivity.LoginActivity;
import com.project.base.baseproject.Activity.MainActivity.MainActivity;
import com.project.base.baseproject.Activity.WelcomeActivity.WelcomeActivity;
import com.project.base.baseproject.AppCustoms.MyApplication.MyApplication;
import com.project.base.baseproject.AppCustoms.Tool.StringUtil;
import com.project.base.baseproject.R;

import static android.content.pm.PackageManager.PERMISSION_GRANTED;

//应用启动页
public class LoadingActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loading);
        //设置沉侵式状态栏
        StatusBarUtil.setColor(this, getResources().getColor(R.color.black),0);
        //判断是否有文件读写权限，因为这个权限几乎是必须的
        if(ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,},5001);
        } else {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    SystemClock.sleep(2000);
                    gotoMainViewController();
                }
            }).start();
        }
    }
    //权限申请结果
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == 5001) {
            int resultCode = grantResults[0];
            //如果用户拒绝了 跳转到设置界面  结束当前应用 因为这个权限是必须要的
            if(resultCode != PERMISSION_GRANTED) {
                AlertDialog.Builder builder1 = new AlertDialog.Builder(LoadingActivity.this);
                builder1.setMessage("请开启文件读写权限");
                builder1.setPositiveButton("前往", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //跳转到设置界面
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        intent.setData(Uri.fromParts("package", getApplicationInfo().packageName, null));
                        //结束当前应用
                        finish();
                        startActivity(intent);
                    }
                });
                builder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                });
                builder1.create().show();
            } else {
                gotoMainViewController();
            }
        }
    }
    //进入判断逻辑 应用启动后应该进入哪个界面
    private void gotoMainViewController() {
        //是不是第一次使用软件  是就进入欢迎界面
        if(MyApplication.identityManager.identity.firstUseSoft == 1) {
            Intent intent = new Intent(LoadingActivity.this,WelcomeActivity.class);
            startActivity(intent);
            finish();
            return;
        }
        //是否需要登陆
        if(StringUtil.isBlank(MyApplication.identityManager.identity.user_guid)) {
            Intent intent = new Intent(LoadingActivity.this,LoginActivity.class);
            startActivity(intent);
            finish();
            return;
        }
        //进入首页
        Intent intent = new Intent(LoadingActivity.this,MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}
